'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    const { ctx } = this;

    let user = ctx.session.uid

    if (user) {
      const getUserInfo = await ctx.service.home.find(user.ID)
      const getIdAllCount = await ctx.service.home.getIdAllCount(getUserInfo)
      let pageNum = Math.ceil(getIdAllCount.count / 5)
      let page=ctx.query.page || 1
      const getNote = await ctx.service.home.getNote(getUserInfo,page)

      await ctx.render('home.html', {
        user: getUserInfo,
        note: getNote,
        pageNum: pageNum
      })

    } else {
      const getAllCount = await ctx.service.home.getAllCount()
      let page=ctx.query.page || 1
      const getAllNote = await ctx.service.home.getAllNote(page)
      let pageNum = Math.ceil(getAllCount.count / 5)

      await ctx.render('home.html', {
        user: '',
        note: getAllNote,
        pageNum: pageNum
      });
    }
  }
}

module.exports = HomeController;
